package com.zh.config.mybatis;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Date;

/**
 * @Description: mybatisPlus 创建时间自动添加，只需要在字段上加上@TableField(fill = FieldFill.INSERT)注解即可
 * @ClassName MetaObjectHandlerConfig
 * @date: 2021.05.10 14:52
 * @Author: zhanghang
 */
@Configuration
public class MetaObjectHandlerConfig {

    @Bean
    public MetaObjectHandler metaObjectHandler(){
        return new MetaObjectHandler() {
            // 自动添加
            @Override
            public void insertFill(MetaObject metaObject) {
                this.setFieldValByName("createTime",new Date(),metaObject);
            }

            @Override
            public void updateFill(MetaObject metaObject) {
                this.setFieldValByName("updateTime",new Date(),metaObject);
            }
        };
    }
}
